package com.llmcu.graphic.designpatterns.command.test;

import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * xxxx
 *
 * @author liuling
 * @date 2024/7/18 23:30
 */
@Getter
@Setter
public class MyJFrame extends JFrame implements WindowListener, MouseMotionListener, ActionListener {
    Box mainBox = new Box(BoxLayout.Y_AXIS);
    Box buttonBox = new Box(BoxLayout.X_AXIS);
    private MyCanvas canvas;
    private MyButton clearButton;
    private String title;

    public MyJFrame(MyCanvas canvas, MyButton clearButton, String title) throws HeadlessException {
        super(title);

        this.canvas = canvas;
        this.clearButton = clearButton;
        this.title = title;

        this.addWindowListener(this);
        canvas.addMouseMotionListener(this);
        clearButton.addActionListener(this);

        buttonBox.add(clearButton);
        mainBox.add(buttonBox);
        mainBox.add(canvas);
        this.getContentPane().add(mainBox);

        this.pack();
        this.show();

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        canvas.draw(e.getX(),e.getY());
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        canvas.repaint();
    }
}
